package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.PicStyle;
import ex.com.jm.food.service.AdminPicStyleService;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.JsonUtils;
/**
 * 尺寸管理action
 * @author zhaojw
 *
 */
public class AdminPicStyleAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private PicStyle picStyle;
	private AdminPicStyleService adminPicStyleService;
	private Integer styleId;
	private int initPage=1;
	private int pageRow=10;
	private int totalCount;
	private String styleValue;
	private String order;
	private String sort;
	public PicStyle getPicStyle() {
		return picStyle;
	}
	public void setPicStyle(PicStyle picStyle) {
		this.picStyle = picStyle;
	}
	public AdminPicStyleService getAdminPicStyleService() {
		return adminPicStyleService;
	}
	public void setAdminPicStyleService(AdminPicStyleService adminPicStyleService) {
		this.adminPicStyleService = adminPicStyleService;
	}
	public Integer getStyleId() {
		return styleId;
	}
	public void setStyleId(Integer styleId) {
		this.styleId = styleId;
	}
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public String getStyleValue() {
		return styleValue;
	}
	public void setStyleValue(String styleValue) {
		this.styleValue = styleValue;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	/**
	 * 到达尺寸列表页面
	 * @return
	 */
	public String picStyleList(){
		return "picStyleList";
	}
	/**
	 * 得到列表数据
	 */
	public void getPicStyles(){
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/javascript");
		int offset=(initPage-1)*pageRow;
		List<PicStyle> picStyles=this.getAdminPicStyleService().getPicStyles(offset, pageRow, styleValue, sort, order);
		JSONArray style=JSONArray.fromObject(picStyles);
		totalCount=this.getAdminPicStyleService().picCount(styleValue);
		JSONObject jo=JsonUtils.buildJson(style, initPage, pageRow, totalCount);
		PrintWriter out=null;
		try {
			out=response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(jo);
		out.flush();
		out.close();
	}
	/**
	 * 到达尺寸信息页
	 * @return
	 */
	public String editPicStyle(){
		if(styleId!=null){
			picStyle=this.getAdminPicStyleService().editPicStyle(styleId);
		}
		return "edit";
		
	}
	/**
	 * 保存或则更新信息
	 * @return
	 */
	public String savePicStyle(){
		if(picStyle.getStyleValue()==null||picStyle.getStyleValue().equals("")){
			addFieldError("picStyle.styleValue", "尺寸值不为空");
			return "edit";
		}else if (picStyle.getStylePrice()==null||picStyle.getStylePrice().equals("")) {
			addFieldError("picStyle.stylePrice", "尺寸价格不为空");
			return "edit";
		}
		PicStyle checkValue=this.getAdminPicStyleService().checkValue(styleValue);
		if(checkValue!=null&&styleId==null){
			addFieldError("picStyle.styleValue", "尺寸值已添加");
			return "edit";
		}
		if(picStyle.getStyleId()==null||picStyle.getStyleId().equals("")){
			picStyle.setCreated(DateUtils.getCurrentDate());
		}
		if(picStyle.getStyleId()!=null&&!picStyle.getStyleId().equals("")){
			this.getAdminPicStyleService().updatePicStyle(picStyle);
			return "picStyleList";
		}
		this.getAdminPicStyleService().savePicStyle(picStyle);
		return "picStyleList";
	}
	/**
	 * 检验尺寸值是否重复
	 */
	public void checkValue(){
		PicStyle checkValue=this.getAdminPicStyleService().checkValue(styleValue);
		String msg="";
		if(checkValue!=null&&styleId==null){
			msg="尺寸值已添加";
		}else {
			msg="尺寸值可以添加";
		}
		JSONArray jo=JsonUtils.buildMessage(msg);
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/javascript");
		PrintWriter out=null;
		try {
			out=response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(jo);
		out.flush();
		out.close();
	}
	
	public String delPicStyle(){
		this.getAdminPicStyleService().delPicStyle(styleId);
		return "picStyleList";
		
	}
}
